//
//  StylePickView.m
//  规格选择
//
//  Created by 徐宏飞 on 16/5/7.
//  Copyright © 2016年 徐宏飞. All rights reserved.
//

#import "StylePickView.h"
#import "Pick_ContentView.h"

#define WID [[UIScreen mainScreen] bounds].size.width / 375
#define HEIGHT [[UIScreen mainScreen] bounds].size.height

@interface StylePickView ()<ContentViewDelegate>

@property (nonatomic, strong) Pick_ContentView *contentV;

@property (nonatomic, strong) UIButton *tapCloseButton;
@property (nonatomic, assign) BOOL openState;

@end

@implementation StylePickView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews{
    
    [self prepareingLeftView];
    
    [self openingLeftController];
}


#pragma mark -- 准备菜单视图
- (void)prepareingLeftView{
    
    if (!_tapCloseButton) {
        self.tapCloseButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.tapCloseButton.frame =[UIScreen mainScreen].bounds;
        self.tapCloseButton.backgroundColor = [UIColor darkGrayColor];
        self.tapCloseButton.alpha = 0;
        [self.tapCloseButton addTarget:self action:@selector(closeingLeftControllerWithMsg:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.tapCloseButton];
    }
    
    if (!_contentV) {
        _contentV = [[[NSBundle mainBundle] loadNibNamed:@"Pick_ContentView" owner:self options:nil]firstObject];
        _contentV.frame = CGRectMake( 0, 667*WID, 375*WID, 400*WID);
        _contentV.delegete = self;
//        [self showShadow:YES];  //阴影效果
        [self addSubview:_contentV];
        self.openState = NO;
    }
}

#pragma mark -- 打开菜单视图
- (void)openingLeftController {
    
    
    if (self.openState == YES) {
        return;
    }
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:10 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
        
        self.contentV.frame = CGRectMake(0, 267*WID, 375*WID, 400*WID);
        if (HEIGHT == 480) {
            self.contentV.frame = CGRectMake(0, 167*WID, 375*WID, 400*WID);
        }
        self.tapCloseButton.alpha = 0.7;
        
    } completion:^(BOOL finished) {
        
        self.openState = YES;
        
    }];
    
}

#pragma mark -- 关闭菜单视图
- (void)closeingLeftControllerWithMsg:(NSString *)msg {
    
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:10 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
        
        self.contentV.frame = CGRectMake(0, 667*WID, 375*WID, 400*WID);
        self.tapCloseButton.alpha = 0;
        
    } completion:^(BOOL finished) {
        
        [self.contentV removeFromSuperview];
        self.contentV = nil;
        [self.tapCloseButton removeFromSuperview];
        self.tapCloseButton = nil;
        self.openState = NO;
        
        if ([msg isKindOfClass:[UIButton class]]) {
            [self.delegete disMissStyleViewWithMessage:@"您还没有选择规格"];
        } else {
            [self.delegete disMissStyleViewWithMessage:msg];
        }
        
        
    }];
    
//    [self showShadow:NO];
}

#pragma mark -- 阴影效果
- (void)showShadow:(BOOL)val {
    if (!self.contentV) return;
    
    self.contentV.layer.shadowOpacity = val ? 0.8f : 0.0f;
    if (val) {
        self.contentV.layer.cornerRadius = 4.0f;
        self.contentV.layer.shadowOffset = CGSizeZero;
        self.contentV.layer.shadowRadius = 4.0f;
        self.contentV.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;
    }
    
}

#pragma mark -- 代理
- (void)disMissCurrentPageWithMessage:(NSString *)msg{
    [self closeingLeftControllerWithMsg:msg];
}


@end
